package com.mingmen.supplier.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.supplier.R;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.defineview.ClearEditText;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.Mobile;
import com.mingmen.supplier.util.SharePreferenceUtil;
import com.mingmen.supplier.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import okhttp3.Call;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.edit_Phone)
    ClearEditText editPhone;
    @BindView(R.id.edit_yanzheng)
    ClearEditText editYanzheng;
    @BindView(R.id.btn_getcode)
    Button btnGetcode;

    @BindView(R.id.btn_register)
    Button btnRegister;


    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;
    @BindView(R.id.text_register)
    TextView textRegister;


    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    @BindView(R.id.text_login)
    TextView text_login;

    private int reckonTime = 60;
    //    验证码倒计时
    private Handler mReckonHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0x1) {
                btnGetcode.setText("(" + reckonTime + ")");
                reckonTime--;
                if (reckonTime < 0) {
                    mReckonHandler.sendEmptyMessage(0x2);
                } else {
                    mReckonHandler.sendEmptyMessageDelayed(0x1, 1000);
                }
            } else if (msg.what == 0x2) {
                // 60秒结束
                btnGetcode.setText("发送");
                btnGetcode.setEnabled(true);
                reckonTime = 60;
            }
        }
    };

    String id_canting, id_gangwei;
    String restruantData;

    private int resurtResurant = 100;
    private int resurtPosition = 200;
    //添加商品101
//
    @BindView(R.id.check_supplier)
    CheckBox check_supplier;
    int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mToolbar.setBackground(getResources().getDrawable(R.color.transparent));
        textCenter.setText("注册");
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        //点击edit 输入框对光标进行处理 这里面对触摸事件进行自定义 并操作密码明文密文

        check_supplier.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    flag = 1;
                } else {
                    flag = 0;
                }


            }
        });
    }


    @OnClick({R.id.edit_Phone, R.id.edit_yanzheng, R.id.btn_getcode, R.id.btn_register, R.id.text_return, R.id.text_center, R.id.linear_return, R.id.text_login})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {

            case R.id.edit_Phone:
                break;
            case R.id.edit_yanzheng:
                break;
            case R.id.btn_getcode:
                if (TextUtils.isEmpty(editPhone.getText().toString())) {
                    ToastUtil.showToast(RegisterActivity.this, "手机号不能为空");
                } else if (!Mobile.isMobile(editPhone.getText().toString())) {
                    ToastUtil.showToast(RegisterActivity.this, "请输入正确手机号");
                } else {

                    getcode();
                }


                break;

            case R.id.btn_register:


                if (TextUtils.isEmpty(editPhone.getText().toString().trim())) {
                    ToastUtil.showToast(RegisterActivity.this, "手机号不能为空");
                } else if (!Mobile.isMobile(editPhone.getText().toString().trim())) {
                    ToastUtil.showToast(RegisterActivity.this, "请输入正确手机号");
                } else if (TextUtils.isEmpty(editYanzheng.getText().toString().trim())) {
                    ToastUtil.showToast(RegisterActivity.this, "验证码不能为空");
                } else if (flag == 0) {
                    ToastUtil.showToast(RegisterActivity.this, "请阅读并同意供应商协议");
                } else {
//                    Register();
                    intent = new Intent(RegisterActivity.this, StoreInformaticaActivity.class);
                    intent.putExtra("verifyCode", editYanzheng.getText().toString());
                    intent.putExtra("editPhone", editPhone.getText().toString());
                    startActivity(intent);
                }

                break;
            case R.id.text_register:

                break;
            case R.id.text_return:

            case R.id.text_login:
            case R.id.linear_return:
                finish();
                break;
        }
    }

    private void getcode() {
        HttpResponse.getCode(editPhone.getText().toString(), "2", new StringCallback(RegisterActivity.this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                BaseBean baseBean = GsonUtil.GsonToBean(s, BaseBean.class);


                if (baseBean.getCode() == 0) {
                    btnGetcode.setEnabled(false);
                    // 开始计时
                    mReckonHandler.sendEmptyMessage(0x1);
                    Toast.makeText(RegisterActivity.this, "验证码发送成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(RegisterActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    private String comPanyName, companyId;
    String position, positionName;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == resurtResurant) {
            comPanyName = data.getStringExtra("companyName");
            companyId = data.getStringExtra("companyId");
            Log.d("dkdkdk", comPanyName);
            Log.d("dkdkdk", companyId);

            SharePreferenceUtil.put(RegisterActivity.this, "companyId", companyId);

        } else if (requestCode == resurtPosition) {
            positionName = data.getStringExtra("companyName");
            Log.d("dkdkdk", positionName);
            position = data.getStringExtra("position");

        }
    }


    public void onEventMainThread(Integer integer) {
        if (integer == 10) {
            finish();
        }

    }
}




